<!DOCTYPE html>
<html class="x-admin-sm">
    <head>
        <meta charset="UTF-8">
        <title>用户列表页</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <meta name="csrf-token" content="{{csrf_token()}}">
        @include('admin.public.style')
        @include('admin.public.script')
    </head>
    <body>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">
                            <form class="layui-form layui-col-space5" method="get" action="{{url('admin/user')}}">
                                {{--<div class="layui-inline layui-show-xs-block">--}}
                                    {{--<input class="layui-input"  autocomplete="off" placeholder="开始日" name="start" id="start">--}}
                                {{--</div>--}}
                                {{--<div class="layui-inline layui-show-xs-block">--}}
                                    {{--<input class="layui-input"  autocomplete="off" placeholder="截止日" name="end" id="end">--}}
                                {{--</div>--}}
                                <div class="layui-input-inline">
                                    <select name="num" lay-filter="aihao">
                                        <option value="" selected=""></option>
                                        <option value="20" @if($request->input('num') == 20) selected="" @endif>20</option>
                                        <option value="50" @if($request->input('num') == 50) selected="" @endif>50</option>
                                        <option value="100" @if($request->input('num') == 100) selected="" @endif>100</option>
                                    </select>
                                </div>
                                <div class="layui-inline layui-show-xs-inline">
                                    <input type="text" name="email"  placeholder="请输入邮箱" value="{{$request->input('eamil')?$request->input('eamil'):""}}" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-inline layui-show-xs-inline">
                                    <input type="text" name="username" value="{{$request->input('username')?$request->input('username'):""}}" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
                                </div>
                            </form>
                        </div>
                        <div class="layui-card-header">
                            <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
                            <button class="layui-btn" onclick="xadmin.open('添加用户','{{url('admin/user/create')}}',600,400)"><i class="layui-icon"></i>添加</button>
                        </div>
                        <div class="layui-card-body layui-table-body layui-table-main">
                            <table class="layui-table layui-form">
                                <thead>
                                  <tr>
                                    <th>
                                      <input type="checkbox" lay-filter="checkall" name="" lay-skin="primary">
                                    </th>
                                    <th>ID</th>
                                    <th>用户名</th>
                                    <th>邮箱</th>
                                    <th>是否是管理员</th>
                                    <th>状态</th>
                                    <th>创建时间</th>
                                    <th>更新时间</th>
                                    <th>操作</th></tr>
                                </thead>
                                <tbody>
                                @foreach($user as $v)
                                  <tr>
                                    <td>
                                      <input type="checkbox" name="id[]" value="{{$v->id}}"   lay-skin="primary">
                                    </td>
                                      <td>{{$v->id}}</td>
                                    <td>{{$v->username}}</td>
                                    <td>{{$v->email}}</td>
                                    <td>@if($v->is_admin == 0) 否@else 是   @endif</td>
                                    <td class="td-status">
                                      <span class="layui-btn layui-btn-normal layui-btn-mini @if($v->status == 0) layui-btn-disabled  @endif">@if($v->status == 0)已停用@else已启用@endif</span>
                                    </td>
                                      <td>{{$v->created_at}}</td>
                                      <td>{{$v->updated_at}}</td>
                                    <td class="td-manage">
                                      <a onclick="member_stop(this,'{{$v->id}}')" href="javascript:;"  title="@if($v->status == 0)启用@else停用@endif">
                                        <i class="layui-icon">@if($v->status == 0) &#xe62f; @else &#xe601; @endif</i>
                                      </a>
                                      <a title="编辑"  onclick="xadmin.open('编辑','{{url('admin/user/'.$v->id.'/edit')}}',600,400)" href="javascript:;">
                                        <i class="layui-icon">&#xe642; </i>
                                      </a>
                                      {{--<a onclick="xadmin.open('修改密码','{{url('admin/user/pwd')}}',600,400)" title="修改密码" href="javascript:;">--}}
                                        {{--<i class="layui-icon">&#xe631;</i>--}}
                                      {{--</a>--}}
                                      <a title="删除" onclick="member_del(this,'{{$v->id}}')" href="javascript:;">
                                        <i class="layui-icon">&#xe640;</i>
                                      </a>
                                    </td>
                                  </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                        <div class="layui-card-body ">
                            <div class="page">
                                {!! $user->render() !!}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div> 
    </body>
    <script>
      layui.use(['laydate','form'], function(){
        var laydate = layui.laydate;
        var  form = layui.form;


        // 监听全选
        form.on('checkbox(checkall)', function(data){

          if(data.elem.checked){
            $('tbody input').prop('checked',true);
          }else{
            $('tbody input').prop('checked',false);
          }
          form.render('checkbox');
        }); 
        
        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });


      });

       /*用户-停用*/
      function member_stop(obj,id){
          var status = $(obj).parents("tr").find(".td-status").find('span').html();
          console.log(status+'---1')
          if(status == '已停用'){
              status = '启用';
          }else{
              status = '停用';
          }
          console.log(status+'---2')
          layer.confirm('确认要'+status+'吗？',function(index){
              if($(obj).attr('title') == '停用'){

                //发异步把用户状态进行更改
                $(obj).attr('title','停用')
                $(obj).find('i').html('&#xe62f;');

                $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');

                $.ajax({
                    type:'PUT',
                    url:"/admin/user/"+id,
                    dateType:'json',
                    ContentType:'application/x-www-form-urlencode',
                    headers:{
                        "X-CSRF-TOKEN":$('meta[name="csrf-token"]').attr('content')
                    },
                    data:{status:0},
                    success:function(data){
                        layer.msg('已停用!',{icon: 5,time:400},function(){
                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        });
                    }
                })
              }else{
                  $.ajax({
                      type:'PUT',
                      url:"/admin/user/"+id,
                      dateType:'json',
                      headers:{
                          "X-CSRF-TOKEN":$('meta[name="csrf-token"]').attr('content')
                      },
                      ContentType:'application/x-www-form-urlencode',
                      data:{status:1},
                      success:function(data){
                          layer.msg('已启用!',{icon: 1,time:400},function(){
                              // 可以对当前口进行刷新
                              location.reload();
                          });
                      }
                  })
                $(obj).attr('title','启用')
                $(obj).find('i').html('&#xe601;');

                $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
              }
              
          });
      }

      /*用户-删除*/
      function member_del(obj,id){
          layer.confirm('确认要删除吗？',function(index){
              $.ajax({
                  method:'delete',
                  url:"/admin/user/"+id,
                  dateType:'json',
                  headers:{
                      "X-CSRF-TOKEN":$('meta[name="csrf-token"]').attr('content')
                  },
                  success:function(data){
                      layer.msg("删除成功",{
                          icon: 1,
                          time: 400 //2秒关闭（如果不配置，默认是3秒）
                      },
                      function() {
                          // 可以对当前口进行刷新
                          location.reload();
                      });
                  },
              })
          });
      }



      function delAll (argument) {
        var ids = [];
        // 获取选中的id
        $('tbody input').each(function(index, el) {
            if($(this).prop('checked')){
               ids.push($(this).val())
            }
        });
          layer.confirm("确认要删除吗?",function() {
              $.get('/admin/user/delAll',{'ids':ids},function(data){
                    layer.msg("删除成功",{icon:1,time:400},function(){
                        // 可以对当前口进行刷新
                        location.reload();
                    });
              });
          });

      }
    </script>
</html>